<template>
  <router-view />
</template>

<script setup>
import {get} from '@/utils/http'
import { reactive} from 'vue'
import { useRouter } from 'vue-router'
import { localGet  } from '@/utils'
import { getToken, setInfo } from '@/utils/auth'
import { apis } from '@/apis'
const noMenu = ['/login']
const router = useRouter()
const state = reactive({
  defaultOpen: ['1', '2', '3', '4'],
  currentPath: '/',
})

const handleAdminInfo = () => {
  get(apis.LOGON_INFO).then((res) => {
    setInfo(res.data)
  })
}

router.afterEach((to, from) => {
  state.showMenu = !noMenu.includes(to.path)
})

router.beforeEach((to, from, next) => {

  if (to.path === '/login') {
    // 如果路径是 /login 则正常执行
    next()
  } else {
    // 如果不是 /login，判断是否有 token
    if (!getToken()) {
      // 如果没有，则跳至登录页面
      next({ path: '/login' })
    } else {
      // 否则继续执行
      handleAdminInfo()
      next()
    }
  }
  state.currentPath = to.path
})

</script>
<style lang="scss">
.el-menu-item:hover{
  outline: 0 !important;
  color: #3f7226 !important;
}

.el-submenu__title:focus, .el-submenu__title:hover{
  outline: 0 !important;
  color: #fff !important;
  background: #3f7226 !important;
  border-radius: 8px !important;
}
.el-menu-item:hover{
  outline: 0 !important;
  color: #fff !important;
  background: #3f7226 !important;
  border-radius: 8px !important;
}
</style>

